package com.sky.util;

import com.sky.properties.Jwtproperties;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

public class JwtUtils {

    /**
     * 生成JWT令牌方法
     * @return
     */
    public static String createJwt(String SecretKey, Long ttl, Map<String, Object> claims){
        //生成JWT令牌
        String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256, SecretKey)
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + ttl))
                .compact();
        return jwt;
    }

    /**
     * 解析JWT令牌
     * @param jwt
     * @return
     */
    public static Claims parseJwt(String jwt,String SecretKey){
        Claims claims = Jwts.parser().setSigningKey(SecretKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;

    }
}
